/*
 * @Author: qin
 * @Date:   2018-08-28
 * +----------------------------------------------------------------------
 * | admin [ 后台管理系统公共配置信息 ]
 * | 用户模块api
 * +----------------------------------------------------------------------
 */
layui.define(['jquery', 'configure', 'configureCom', 'sessionFun'], function (exports) {
    var $ = layui.jquery;
    var configure = layui.configure;
    var configureCom = layui.configureCom;
    var sessionFun = layui.sessionFun;
    var user = {
        //登录方法
        login: function (data) {
            return new Promise(function (resolve, reject) {
                configureCom.postDataAjax(configure.LOGIN_HTTP, data).then(function (res) {
                    var sessionData = {};
                    sessionData.time = configure.OVERDUE_DATE; // 过期时间
                    sessionData.start = (new Date()).getTime(); // 保存变量时的那个时间点，这里以时间戳为例
                    sessionData.token = res.token; // token

                    sessionFun.set(configure.SESSION_TOKEN, sessionData);
                    localStorage.getToken = res.token;
                    localStorage.SysUser =res.user;
                    localStorage.getRealName =res.user.realName;
                    resolve({code: 1, msg: '登录成功', result: {}});
                }, function (err) {
                    reject({code: 0, msg: '用户名或密码错误', result: {}});
                });
            });
        },
        loginCloud: function (data) {
            return new Promise(function (resolve, reject) {
                configureCom.postAjax(configure.LOGIN_CLOUD_HTTP, data).then(function (res) {
                    var sessionData = {};
                    sessionData.time = configure.OVERDUE_DATE; // 过期时间
                    sessionData.start = (new Date()).getTime(); // 保存变量时的那个时间点，这里以时间戳为例
                    sessionData.token = res.id_token; // token
                    sessionFun.set(configure.SESSION_TOKEN, sessionData);
                    resolve({code: 1, msg: '登录成功', result: {}});
                }, function (err) {
                    reject({code: 0, msg: '用户名或密码错误', result: {}});
                });
            });
        },
        //退出
        loginOut: function (data) {
            return new Promise(function (resolve, reject) {
                /*   sessionFun.clear();
                   resolve({code: 1, msg: 'succ',result:{}});*/
                configureCom.postDataAjax(configure.LOGIN_OUT_HTTP, data).then(function (res) {
                    sessionFun.clear();
                    resolve({code: 0, msg: 'succ', result: {}});
                }, function (err) {
                    reject({code: 2, msg: 'err', result: {}});
                });
            });
        }
    };
    exports('user', user);
});